package io.lwy.proxy.config;

import com.google.common.collect.ImmutableMap;
import io.lwy.proxy.httpproxy.MyProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


import javax.servlet.Servlet;
import java.util.Map;

/**
 * @Description: 实现代理配置
 * @Parameters:
 * @Return:
 * @CreateDate: 2019/7/19 9:52
 * @Version: V1.00
 * @Author: jetlee
 */
@Configuration
@EnableAutoConfiguration
@PropertySource("classpath:proxy-config.properties")
public class SolrProxyServletConfiguration {

    // 读取配置文件中路由设置
    @Value("${proxy.servlet_url}")
    private String servlet_url;
    // 读取配置中代理目标地址
    @Value("${proxy.target_url}")
    private String target_url;


    @Bean
    public Servlet createProxyServlet() {
        // 创建新的ProxyServlet
        return new MyProxyServlet();
    }

    @Bean
    public ServletRegistrationBean proxyServletRegistration() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(createProxyServlet(), servlet_url);
        //设置网址以及参数
        Map<String, String> params = ImmutableMap.of(
                "targetUri", target_url,
                "log", "true");
        registrationBean.setInitParameters(params);
        return registrationBean;
    }
}